package com.example.spinning.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spinning.bean.MaterialQuality;
import com.example.spinning.bean.PaginationResult;
import com.example.spinning.mapper.MaterialQualityMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;

@Service
@Transactional
public class MaterialQualityService {
    @Autowired
    MaterialQualityMapper materialQualityMapper;


    //增加操作
    public int addNewMaterialQuality(MaterialQuality materialQuality) {

        return materialQualityMapper.addNewMaterialQuality(materialQuality);
    }

    //删除操作
    public boolean deleteMaterialQualityById(Long[] ids) {
        int result = materialQualityMapper.deleteMaterialQualityById(ids);
        return result == ids.length;
    }


    //查询并分页
    public PaginationResult<MaterialQuality> findPage(Integer page, Integer size, String batch_no, String name, Date date, String origin, String colour) {

        QueryWrapper<MaterialQuality> wrapper = new QueryWrapper();
        if (null!=batch_no)
            wrapper.eq("batch_no", batch_no);
        if (null!=name)
            wrapper.eq("name", name);
        if (null!=date)
            wrapper.eq("date", date);
        if (null!=origin)
            wrapper.eq("origin", origin);
        if (null!=colour)
            wrapper.eq("colour", colour);

        Page<MaterialQuality> pages = new Page<>(page,size);
        materialQualityMapper.selectPage(pages, wrapper);
        int total = materialQualityMapper.selectCount(wrapper);
        List<MaterialQuality> list = pages.getRecords();

        PaginationResult<MaterialQuality> pr = new PaginationResult<>();
        pr.setTotal(total);
        pr.setList(list);
        return pr;
    }
    //动态修改
    public int updateCategoryById(MaterialQuality materialQuality){

         int result = materialQualityMapper.updateById(materialQuality);

        return result;
    }



}
